package com.acme.bankapp.domain.bank;

/**
 * Abstract account contains information about balance
 * @author Artem Nikitin
 *
 */
public abstract class AbstractAccount implements Account {

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		if (Double.doubleToLongBits(balance) != Double
				.doubleToLongBits(other.balance))
			return false;
		if (id != other.id)
			return false;
		return true;
	}

	protected int id;
	protected double balance;

	/**
	 * Constructs a new account with specified balance
	 * @param amount - balance
	 */
	public AbstractAccount(int id, final double amount) {
		if (amount < 0) {
			throw new IllegalArgumentException("Balance cannot be negative");
		}
		this.balance = amount;
		this.id = id;
	}

	public double getBalance() {
		return balance;
	}

	public abstract void deposit(final double amount);

	public abstract void withdraw(final double amount) throws NotEnoughFundsException;
	
	@Override
	public String toString() {
		return "ID " + id + " Balance: " + getBalance();
	}

}
